<template>
  <!-- 对话框 -->
  <el-dialog title="添加用户" :visible.sync="value" width="30%" @close="resultForm">
    <el-form :model="form" :rules="rules" label-position="left" label-width="100px" ref="form">
      <el-form-item label="用户名" prop="username">
        <el-input autocomplete="off" v-model="form.username" ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input autocomplete="off" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input autocomplete="off" v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input autocomplete="off" v-model="form.mobile"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeForm">取 消</el-button>
      <el-button type="primary" @click="addUser">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import axios from '@/axios/axios'
import rules from '@/mixin/rules.js'
export default {
  name: 'UserForm',
  props: ['value','getUserList'],
  mixins:[rules],
  data() {
    return {
      form: {
        username: '',
        password: '',
        email: '',
        mobile: '',
      },
     
    }
  },
  methods: {
    //关闭
    closeForm() {
      this.$emit('input', false)
    },
    //重置表单
    resultForm(){
      this.$refs.form.resetFields();
    },
    //表单提交
    addUser(){
      this.$refs.form.validate(async (valid)=>{
        if(!valid) return
        const {data}=await axios.post(`/users`,this.form)
        if(data.meta.status!=201){
         return this.$message.error(data.meta.msg)
        }
        this.$message.success(data.meta.msg)
        this.getUserList()
        this.$emit('input', false)
      })
    }
  },
}
</script>

<style>
</style>